﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using WpfApp1.Models;

namespace WpfApp1.Services
{
    //用于配置文件的更新静态类
    public class ConfigService
    {
        private static string path = Path.GetTempPath() + @"CRDCCollaboPlatConfig\Revit";

        //将本地的工作目录存储在静态变量当中
        public static string localFileFolder = @"E:\gitrepoprojects\FurioTest\WPFfurioGitee\CRDCCollabWPF\localFileFolder";
        /// <summary>
        /// 初始化用户配置
        /// </summary>
        public static void InitConfig()
        {
            try
            {
                //判断是否已存在配置文件夹
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                string UserPath = path + @"\UserInfo.CRDC";

                //判断是否存在用户配置文件
                if (!File.Exists(UserPath))
                {
                    //实例化用户信息类
                    UserInfo userInfo = new UserInfo
                    {
                        AutoLogin = false,
                        IsRemembered = false,
                        User = "",
                        UserId = "",
                        MajorID = "",
                        Pwd = "",
                        ServerPort = "9999",
                        ServerIP = "172.19.133.19",
                    };
                    var json = JsonConvert.SerializeObject(userInfo, Formatting.Indented);
                    File.WriteAllText(UserPath, json);
                }
                //用户的文件夹管理字符串
                string FolderPath = path + @"\FolderInfo.CRDC";
                if (!File.Exists(FolderPath))
                {
                    FileStream fs= File.Create(FolderPath);
                    fs.Close();
                    
                }
                //用户的文档管理字符串
                string DocumentPath = path + @"\DocumentInfo.CRDC";
                if (!File.Exists(DocumentPath))
                {
                    FileStream fs = File.Create(DocumentPath);
                    fs.Close();
                }

                //用户的所属文件夹
                string FolderBelongedPath = path + @"\FolderBelonged.CRDC";
                if (!File.Exists(FolderBelongedPath))
                {
                    FileStream fs = File.Create(FolderBelongedPath);
                    fs.Close();
                }

                //用户的所有文件夹,用户自己所拥有的文件夹
                string FolderOwnedPath = path + @"\FolderOwned.CRDC";
                if (!File.Exists(FolderOwnedPath))
                {
                    FileStream fs = File.Create(FolderOwnedPath);
                    fs.Close();
                }
            }

            catch { }
        }
        /// <summary>
        /// 读取用户配置
        /// </summary>
        /// <param name="settingName"></param>
        /// <returns></returns>
        public static string GetSettingString(string settingName)
        {


            try
            {
                string fileName = Path.GetTempPath() + @"CRDCCollaboPlatConfig\Revit\UserInfo.CRDC";
                using (StreamReader file = File.OpenText(fileName))
                {
                    JsonTextReader reader = new JsonTextReader(file);
                    JObject jsonObject = (JObject)JToken.ReadFrom(reader);
                    return jsonObject[settingName].ToString();
                }



            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 更新用户配置
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="valueName"></param>
        public static void UpdateSettingString(string settingName, string valueName)
        {
            string fileName = Path.GetTempPath() + @"CRDCCollaboPlatConfig\Revit\UserInfo.CRDC";
            string jsonString = File.ReadAllText(fileName, System.Text.Encoding.UTF8);//读取文件
            JObject jobject = JObject.Parse(jsonString);//解析成json
            jobject[settingName] = valueName;//替换需要的文件
            string convertString = Convert.ToString(jobject);//将json装换为string
            File.WriteAllText(fileName, convertString);//将内容写进jon文件中

        }

        /// <summary>
        /// 将某一个字典写入文件文本中
        /// </summary>
        /// <param name="keyValuePairs"></param>
        /// <param name="filePath"></param>
        public static void DictToFile(Dictionary<string,string> keyValuePairs,string filePath)
        {
            File.WriteAllText(filePath, JsonConvert.SerializeObject(keyValuePairs, Formatting.Indented));

        }


        /// <summary>
        /// 将文件中的字典字段读出来
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static Dictionary<string,string> DictFromFile(string filePath)
        {
            using (StreamReader streamReader = new StreamReader(filePath))
            {
                string tempData = streamReader.ReadToEnd();
                if(tempData != null)
                {
                    Dictionary<string, string> keyValuePairs = JsonConvert.DeserializeObject<Dictionary<string,string>>(tempData);
                    return keyValuePairs;
                }
                else
                {
                    return null;
                }
            }

        }
        /// <summary>
        /// 测试在特定的字典中是否包含某一个值
        /// </summary>
        /// <param name="keyValuePairs"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool isContainValue(Dictionary<string, string> keyValuePairs, string value)
        {
            bool Result = false;
            foreach(var item in keyValuePairs)
            {
                if(item.Value == value)
                {
                    Result = true;
                    return Result;
                }
            }
            return Result;
        }
    }
}
